package com.geniusk.system.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.geniusk.common.core.JSONResult;
import com.geniusk.system.model.bo.group.DeleteGroupByIdBO;
import com.geniusk.system.model.bo.group.InsertGroupBO;
import com.geniusk.system.model.bo.group.SelectGroupBO;
import com.geniusk.system.model.bo.group.UpdateGroupBO;
import com.geniusk.system.model.pojo.Group;
import com.geniusk.security.annotation.PreFuncAuth;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织管理接口
 */
@RestController
@RequestMapping("/system/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    /**
     * 查询组织(所有组织)
     */
    @PostMapping("/selectAllGroup")
    public JSONResult selectAllGroup(@Valid @RequestBody SelectGroupBO selectGroupBO) {
        Group group = JSONUtil.parse(selectGroupBO).toBean(Group.class);
        List<Group> list = groupService.selectAllGroup(group);
        Map<String,Object> result = new HashMap<>();
        result.put("list",list);
        return JSONResult.ok(result);
    }

    /**
     * 添加组织
     */
    @PostMapping("/insertGroup")
    @PreFuncAuth("SYS:DEPT:INSERT")
    public JSONResult insertGroup(@Valid @RequestBody InsertGroupBO insertGroupBO) {
        Group group = JSONUtil.parse(insertGroupBO).toBean(Group.class);
        Long groupId = IdUtil.getSnowflake().nextId();
        group.setGroupId(groupId);
        group.setCreateBy(SecurityUtil.getUsername());
        int rows = groupService.insertGroup(group);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 修改组织
     */
    @PostMapping("/updateGroup")
    @PreFuncAuth("SYS:DEPT:UPDATE")
    public JSONResult updateGroup(@Valid @RequestBody UpdateGroupBO updateGroupBO) {
        Group group = JSONUtil.parse(updateGroupBO).toBean(Group.class);
        group.setUpdateBy(SecurityUtil.getUsername());
        int rows = groupService.updateGroup(group);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 删除组织
     */
    @PostMapping("/deleteGroupById")
    @PreFuncAuth("SYS:DEPT:DELETE")
    public JSONResult deleteGroupById(@Valid @RequestBody DeleteGroupByIdBO deleteGroupByIdBO) {
        if (groupService.hasChildByGroupId(deleteGroupByIdBO.getGroupId())) {
            return JSONResult.errorMsg("存在下级组织不允许删除");
        }
        int rows = groupService.deleteGroupById(deleteGroupByIdBO.getGroupId());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
